
AddCSLuaFile('cl_init.lua')
AddCSLuaFile('shared.lua')

include('shared.lua')


function ENT:Initialize()
	
	self.Entity:SetModel("models/weapons/w_hopwire.mdl")
	self.Entity:PhysicsInit(SOLID_VPHYSICS)
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
	self.Entity:SetSolid(SOLID_VPHYSICS)
	self.HookPos = {}
	self.HookEnts = {}
	self.HooksShot = 0
	self.HopHeight = 400
	self.Glow = false
	self.PreDetonate = false
	self.Combat = false
	self.Tether = false
	
	local phys = self.Entity:GetPhysicsObject()
	if phys and phys:IsValid() then
		phys:Wake()
	end
	
	print( self.Entity:GetPos() )
	
	self:SetTimer(self.Entity:GetNWFloat("timer"))

	self.Entity:SetName("TripWire"..self.Entity:EntIndex());
	
end

function ENT:SetTimer(delay)

	self.PreDetonate = true
	self.Entity:NextThink(CurTime() + delay)
	
end

function ENT:CreateRope(ent1, ent2)

	// Make it invisible, but it still needs to be there
	constraint.Rope(ent1, ent2, 0, 0, Vector(0, 0, 0), Vector(0, 0, 0), 32, 64, 0, 0.75, "cable/physbeam", 0)

end

function ENT:Think()
	
	if self.PreDetonate then
		self:Detonate()
		self.PreDetonate = false
	end
	
	if self.Tether then
		
		timer.Simple( 0.3, function() self.Entity:SetMoveType(MOVETYPE_NONE) end )
	
		local vel = VectorRand()
		local hook = ents.Create("tetherhook")
		hook:SetPos(self.Entity:GetPos())
		hook:SetAngles(self.Entity:GetAngles())
		hook:SetOwner(self.Entity)
		hook:Spawn()
		hook:Activate() 
		
		constraint.NoCollide(self.Entity, hook, 0, 0)
		
		hook:SetName("TetherHook"..hook:EntIndex());
		
		self.HooksShot = self.HooksShot + 1
		self.HookEnts[self.HooksShot] = hook
		
		self:CreateRope(self.Entity, hook)
		
		if (self.HooksShot % 2) then
			vel = vel * -1;
		else
			vel = VectorRand()
		end
		
		hook:SetVelocity(vel * 500)
		
		if (self.HooksShot == 8) then
			
			for i = 1, table.getn(self.HookEnts) do
				self.HookEnts[i]:SetNWBool("activated", true)
			end
			
			self.Entity:SetMoveType(MOVETYPE_VPHYSICS)
			
			self.Combat = true
			self.Tether = false
			self.Entity:NextThink(CurTime() + 0.1)
		else
			self.Tether = true
			self.Combat = false
			self.Entity:NextThink(CurTime() + math.Rand(0.1, 0.3))
		end
	end
	
end

function ENT:Explode()

	local xpl = ents.Create("env_explosion")
	xpl:SetKeyValue("iMagnitude", 125)
	xpl:SetKeyValue("iRadiusOverride", 45)
	xpl:SetPos(self.Entity:GetPos())
	xpl:Spawn()
	xpl:Fire("Explode", "", 0.01)
	
	self.Entity:Remove()
	
end

function ENT:Detonate()

	local trace = {}
	trace.start = self.Entity:GetPos()
	trace.endpos = self.Entity:GetPos() + Vector(0, 0, self.HopHeight * 2)
	trace.filter = self.Entity
	trace.mask = MASK_SOLID
	
	local tr = util.TraceLine(trace)
	
	local hopheight = math.min(self.HopHeight, (self.HopHeight * tr.Fraction))
	local hopvel = Vector(0, 0, hopheight)
	print(hopvel)
	
	self.Entity:SetVelocity(hopvel);
	
	local apextime = math.sqrt(hopheight / server_settings.Int("sv_gravity", 600))
	
	self.Tether = true
	self.Entity:NextThink(CurTime() + apextime)
	
end

function ENT:OnRemove()

	for i = 1, #self.HookEnts do
		self.HookEnts[i]:Remove()
	end
	
end
	